package com.asa.rippleanimationview

import android.animation.*
import android.content.Context
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.widget.RelativeLayout
import androidx.core.animation.addListener
import androidx.core.content.ContextCompat
import com.asa.rippleanimationview.ui.UIRelativeLayout
import com.asa.rippleanimationview.ui.UIUtils

/**
 * create By：anderson
 * on 2022/5/8
 * desc:
 */
class RippleAnimationView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : RelativeLayout(context, attrs) {



    var paint = Paint().apply {
        isAntiAlias = true
    }
    var scaleValue = 10f
    private var rippleCircleCount = 0
    private var singleDelayTime = 0L
    private var isRunning = false
    val animators = mutableListOf<Animator>()
    private val viewLists = mutableListOf<View>()
    private val animatorSet = AnimatorSet()
    companion object{
        const val DURUTION_TIME = 3500L
    }


    init {

        val obtainStyledAttributes =
            context.obtainStyledAttributes(attrs, R.styleable.RippleCircleView)
        val radius = obtainStyledAttributes.getInt(R.styleable.RippleCircleView_radius, 60)

        val color = obtainStyledAttributes.getColor(R.styleable.RippleCircleView_ripple_color,ContextCompat.getColor(context,R.color.red))
        val type = obtainStyledAttributes.getInt(R.styleable.RippleCircleView_ripple_type, 0)
        if (type == 0) {
            paint.style = Paint.Style.FILL
        } else{
            val strokeWidth =
                obtainStyledAttributes.getFloat(R.styleable.RippleCircleView_ripple_strokeWidth, 10f)
            paint.style = Paint.Style.STROKE
            paint.strokeWidth = strokeWidth
        }
        paint.color = color
        scaleValue = obtainStyledAttributes.getFloat(R.styleable.RippleCircleView_scale,10f)
        rippleCircleCount = obtainStyledAttributes.getInt(R.styleable.RippleCircleView_ripple_count,10)
        singleDelayTime = DURUTION_TIME/rippleCircleCount
        obtainStyledAttributes.recycle()


        for (i  in  0 until rippleCircleCount){
            val rippleCircleView = RippleCircleView(this)
            val layoutParams = LayoutParams(UIUtils.getInstance().getWidth(radius),UIUtils.getInstance().getHeight(radius))
            layoutParams.addRule(CENTER_IN_PARENT, TRUE)
            addView(rippleCircleView,layoutParams)


            val scaleX = PropertyValuesHolder.ofFloat("scaleX",scaleValue,1f)
            val scaleY = PropertyValuesHolder.ofFloat("scaleY",scaleValue,1f)
            val alpha = PropertyValuesHolder.ofFloat("alpha",0f,1f)

            val ofPropertyValuesHolder = ObjectAnimator.ofPropertyValuesHolder(rippleCircleView,scaleX, scaleY, alpha)
            ofPropertyValuesHolder.duration = DURUTION_TIME
            ofPropertyValuesHolder.repeatCount = ValueAnimator.INFINITE
            ofPropertyValuesHolder.repeatMode = ValueAnimator.RESTART
            ofPropertyValuesHolder.startDelay = (singleDelayTime * i)
            animators.add(ofPropertyValuesHolder)
            viewLists.add(rippleCircleView)
        }
    }

    fun startAnimRunning(){
        if (!isRunning){
            if (animators.isNotEmpty()){
                val childAnimations = animatorSet.childAnimations
                childAnimations.forEach{ animator->
                    val objectAnimator = animator as ObjectAnimator
                    objectAnimator.repeatCount = ValueAnimator.INFINITE
                }
                viewLists.forEach {
                    it.visibility = View.VISIBLE
                }
                animatorSet.playTogether(animators)
                animatorSet.interpolator = AccelerateInterpolator()
                animatorSet.start()
                isRunning = true
            }
        }
    }

    fun stopAnimRunning(){
        if (isRunning){
            val childAnimations = animatorSet.childAnimations
            childAnimations.forEach{ animator->
                val objectAnimator = animator as ObjectAnimator
                objectAnimator.repeatCount = 0
            }


            var endCount = 0

            animatorSet.addListener(object :Animator.AnimatorListener{
                override fun onAnimationStart(animation: Animator?) {

                }

                override fun onAnimationEnd(animation: Animator?) {
                      endCount ++
                    if (endCount == rippleCircleCount){
                        animatorSet.end()
                        isRunning = false
                    }
                }

                override fun onAnimationCancel(animation: Animator?) {
                }

                override fun onAnimationRepeat(animation: Animator?) {
                }

            })
        }
    }

    fun isRunning() = isRunning



}